/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.layer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;

import twaver.Element;
import twaver.ElementAttribute;
import twaver.Layer;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.table.TElementTable;

public class TablePanel extends JPanel {

	private TDataBox tableBox;
	private TDataBox originalBox;
	private JTabbedPane tabbedPane = new JTabbedPane();

	public TablePanel(TDataBox tableBox, TDataBox originalBox) {
		this.tableBox = tableBox;
		this.originalBox = originalBox;
		initGUI();
	}

	private void initGUI() {
		this.setLayout(new BorderLayout());
		JPanel customDrawPanel = getCustomDrawPanel();
		tabbedPane.addTab("CustomDraw", customDrawPanel);
		JPanel layerPanel = getLayerPanel();
		tabbedPane.addTab("    Layer   ", layerPanel);		
		this.add(tabbedPane, BorderLayout.CENTER);
	}

	private JPanel getLayerPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		List list = new ArrayList();
		list.add(createAttribute(Layer.PROPERTY_ID, TWaverUtil.getString("layer." + Layer.PROPERTY_ID), null, false));
		list.add(createAttribute(Layer.PROPERTY_NAME, TWaverUtil.getString("layer." + Layer.PROPERTY_NAME), String.class, false));
		list.add(createAttribute(Layer.PROPERTY_VISIBLE, TWaverUtil.getString("layer." + Layer.PROPERTY_VISIBLE), Boolean.class, true));
		list.add(createAttribute(Layer.PROPERTY_SELECTABLE, TWaverUtil.getString("layer." + Layer.PROPERTY_SELECTABLE), Boolean.class, true));
		list.add(createAttribute(Layer.PROPERTY_MOVABLE, TWaverUtil.getString("layer." + Layer.PROPERTY_MOVABLE), Boolean.class, true));
		list.add(createAttribute(Layer.PROPERTY_RESIZABLE, TWaverUtil.getString("layer." + Layer.PROPERTY_RESIZABLE), Boolean.class, true));
		list.add(createAttribute(Layer.PROPERTY_ALPHA, TWaverUtil.getString("layer." + Layer.PROPERTY_ALPHA), Float.class, true));
		list.add(createAttribute(Layer.PROPERTY_DESCRIPTION, TWaverUtil.getString("layer." + Layer.PROPERTY_DESCRIPTION), String.class, true));
		TElementTable table = getTable(list);
		panel.add(getContrlPane(table), BorderLayout.NORTH);
		panel.add(new JScrollPane(table), BorderLayout.CENTER);
		return panel;
	}

	private JPanel getCustomDrawPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		List list = new ArrayList();
		String prefix = "twaver.BaseElement.";
		list.add(createAttribute(
					TWaverConst.PROPERTYNAME_CUSTOM_DRAW_SHAPE_FACTORY,
					TWaverUtil.getString(prefix + TWaverConst.PROPERTYNAME_CUSTOM_DRAW_SHAPE_FACTORY),
					null,
					"twaver.table.editor.EnumTypeEditor@twaver.shape|false",
					"twaver.table.renderer.EnumTypeRenderer@twaver.shape",
					false));
		list.add(createAttribute(
					TWaverConst.PROPERTYNAME_CUSTOM_DRAW_GRADIENT_FACTORY,
					TWaverUtil.getString(prefix + TWaverConst.PROPERTYNAME_CUSTOM_DRAW_GRADIENT_FACTORY),
					null,
					"twaver.table.editor.EnumTypeEditor@twaver.gradient|false",
					"twaver.table.renderer.EnumTypeRenderer@twaver.gradient",
					true));
		list.add(createAttribute(
					TWaverConst.PROPERTYNAME_CUSTOM_DRAW_FILL_COLOR,
					TWaverUtil.getString(prefix + TWaverConst.PROPERTYNAME_CUSTOM_DRAW_FILL_COLOR),
					Color.class, true));
		list.add(createAttribute(TWaverConst.PROPERTYNAME_CUSTOM_DRAW_GRADIENT,
					TWaverUtil.getString(prefix + TWaverConst.PROPERTYNAME_CUSTOM_DRAW_GRADIENT),
					Boolean.class,
					true));
		list.add(createAttribute(
					TWaverConst.PROPERTYNAME_CUSTOM_DRAW_GRADIENT_COLOR,
					TWaverUtil.getString(prefix + TWaverConst.PROPERTYNAME_CUSTOM_DRAW_GRADIENT_COLOR),
					Color.class,
					true));
		list.add(createAttribute(
					TWaverConst.PROPERTYNAME_CUSTOM_DRAW_OUTLINE_COLOR,
					TWaverUtil.getString(prefix + TWaverConst.PROPERTYNAME_CUSTOM_DRAW_OUTLINE_COLOR),
					Color.class, true));
		list.add(createAttribute(
				TWaverConst.PROPERTYNAME_CUSTOM_DRAW_OUTLINE_STROKE,
				TWaverUtil.getString(prefix + TWaverConst.PROPERTYNAME_CUSTOM_DRAW_OUTLINE_STROKE),
				Stroke.class,
				"twaver.table.editor.StrokeEditor",
				"twaver.table.renderer.StrokeRenderer",
				true));
		TElementTable table = getTable(list);
		panel.add(getContrlPane(table), BorderLayout.NORTH);
		panel.add(new JScrollPane(table), BorderLayout.CENTER);
		return panel;
	}

	private TElementTable getTable(List list) {
		TElementTable table = new TElementTable(tableBox);
		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setIteratorByHiberarchy(true);
		table.setElementClass(Node.class);
		table.registerElementClassAttributes(Node.class, list);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		return table;
	}

	private ElementAttribute createAttribute(String key, String displayName, Class clazz, boolean editable) {
		return createAttribute(key, displayName, clazz, null, null, editable);
	}

	private ElementAttribute createAttribute(String key, String displayName, Class clazz, String editor, String renderer, boolean editable) {
		ElementAttribute attribute = new ElementAttribute();
		attribute.setClientPropertyKey(key);
		attribute.setDisplayName(displayName);
		attribute.setEditable(editable);
		attribute.setJavaClass(clazz);
		if (editor != null) {
			attribute.setEditorClass(editor);
		}
		if (renderer != null) {
			attribute.setRendererClass(renderer);
		}
		return attribute;
	}

	public JToolBar getContrlPane(final TElementTable table) {
		JToolBar toolbar = new JToolBar();
		JButton topButton = createButton("top.png", "top");
		JButton upButton = createButton("up.png", "up");
		JButton downButton = createButton("down.png", "down");
		JButton bottomButton = createButton("bottom.png", "bottom");
		final JCheckBox editableCheckBox = new JCheckBox(TWaverUtil.getString("EDITABLE"), false);

		toolbar.setMinimumSize(TWaverConst.EMPTY_DIMENSION);
		toolbar.setRollover(true);
		toolbar.setFloatable(false);

		toolbar.addSeparator();
		toolbar.add(topButton);
		toolbar.add(upButton);
		toolbar.add(downButton);
		toolbar.add(bottomButton);

		editableCheckBox.setSelected(table.isEditable());
		editableCheckBox.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				table.setEditable(editableCheckBox.isSelected());
			}
		});
		editableCheckBox.setFocusPainted(false);
		toolbar.addSeparator();
		toolbar.add(editableCheckBox);
		return toolbar;
	}

	protected JButton createButton(String image, String actionCommand) {
		Icon icon = TWaverUtil.getIcon("/resource/image/table/" + image);
		JButton button = new JButton(icon);
		button.setMargin(TWaverConst.NONE_INSETS);
		button.setActionCommand(actionCommand);
		button.addActionListener(listener);
		return button;
	}

	private ActionListener listener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			String action = e.getActionCommand();
			Element element = tableBox.getLastSelectedElement();
			if (element == null) {
				return;
			}
			Layer layer = (Layer) element.getID();
			if ("top".equals(action)) {
				originalBox.getLayerModel().moveToTop(layer);
				tableBox.moveToTop(element);
			}
			else if ("up".equals(action)) {
				originalBox.getLayerModel().moveToUp(layer);
				tableBox.moveToUp(element);
			}
			else if ("down".equals(action)) {
				originalBox.getLayerModel().moveToDown(layer);
				tableBox.moveToDown(element);
			}
			else if ("bottom".equals(action)) {
				originalBox.getLayerModel().moveToBottom(layer);
				tableBox.moveToBottom(element);
			}
		}
	};

}